
/**
 * Created by dqw on 2015/12/30.
 */

//列表开始
var dtGridColumns = [

    {
        id: 'status',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'eq',
        hideQueryValue:6,
        hide:true
    },
    {
        id: 'id',
        title: 'ID',
        type: 'number',
        columnClass: 'text-center width-20',
        fastSort: true,
        fastQuery: true,
        fastQueryType: 'eq',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            return '<a href="' + ncGlobal.adminRoot + 'orderHold/look?id=' + record.id + '">' + value + '</a>';
        }
    },
    {
        id: 'userId',
        title: '客户ID',
        type: 'number',
        headerClass: 'text-left',
        columnClass: 'text-left width-20',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            return '<a href="' + ncGlobal.adminRoot + 'user/look?id=' + record.userId + '">' + value + '</a>';
        }
    },
    {
        id: 'inviterId',
        title: '上级ID',
        type: 'number',
        headerClass: 'text-left',
        columnClass: 'text-left width-20',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            return '<a href="' + ncGlobal.adminRoot + 'user/look?id=' + record.inviterId + '">' + value + '</a>';
        }
    },
    {
        id: 'u.userName',
        title: '客户姓名',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left width-20',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            return '<a href="' + ncGlobal.adminRoot + 'orderHold/look?id=' + record.id + '">' + value + '</a>';
        }
    },
    // {
    //     id: 'phone',
    //     title: '电话',
    //     type: 'string',
    //     headerClass: 'text-left',
    //     columnClass: 'text-left',
    //     fastSort: false
    // },
    {
        id: 'equityCode',
        title: '股票代码',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },

    {
        id: 'equityName',
        title: '股票名称',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'tradeCredit',
        title: '点买额度(万)',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: true,
        fastQuery: true,
        fastQueryType: 'range',
        format:'#,##0'
    },
    // {
    //     id: 'buyPrice',
    //     title: '点买价格',
    //     type: 'number',
    //     headerClass: 'text-center',
    //     columnClass: 'text-right',
    //     fastSort: true,
    //     fastQuery: true,
    //     fastQueryType: 'range',
    //     format:'#,##0.00'
    // },
    {
        id: 'buyNumber',
        title: '点买数量',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: true,
        format:'#,##0'
    },
    {
        id: 'buyDealPrice',
        title: '点买成交价格',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: true,
        format:'#,##0.000',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            return '<span style="padding:2px 5px;color:red;font-size: large" >' + value + '</span>';
        }
    },
    {
        id: 'buyDealNumber',
        title: '买入成交数量',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: true,
        format:'#,##0'
    },
    {
        id: 'i.userName',
        title: '投资人姓名',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'sellDealNumber',
        title: '点卖成交数量',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: true,
        format:'#,##0'
    },
    {
        id: 'sellDealPrice',
        title: '点卖成交价格',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: true,
        format:'#,##0.000',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            return '<span style="padding:2px 5px;color:red;font-size: large">' + value + '</span>';
        }
    },
    {
        id: 'profitStopPrice',
        title: '止盈价格',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: true,
        fastQuery: false,
        format: '#,##0.00'
    },
    {
        id: 'loseStopPrice',
        title: '止损价格',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: true,
        fastQuery: false,
        format: '#,##0.00'
    },
    // {
    //     id: 'buyTime',
    //     title: '点买时间',
    //     type: 'date',
    //     format:'yyyy-MM-dd',
    //     headerClass: 'text-left',
    //     columnClass: 'text-left',
    //     fastSort: false,
    //     fastQuery: true,
    //     fastQueryType: 'range'
    // },
    {
        id: 'buyDealTime',
        title: '点买成交时间',
        type: 'date',
        format:'yyyy-MM-dd',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range'
    },
    {
        id: 'acceptTime',
        title: '投资人接单时间',
        type: 'date',
        format:'yyyy-MM-dd',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range'
    },
    {
        id: 'eveningUpTime',
        title: '发起平仓时间',
        type: 'date',
        format:'yyyy-MM-dd',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range'
    },
    // {
    //     id: 'sellTime',
    //     title: '卖出时间',
    //     type: 'date',
    //     format:'yyyy-MM-dd',
    //     headerClass: 'text-left',
    //     columnClass: 'text-left',
    //     fastSort: false,
    //     fastQuery: true,
    //     fastQueryType: 'range'
    // },
    {
        id: 'sellDealTime',
        title: '卖出成交时间',
        type: 'date',
        format:'yyyy-MM-dd',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range'
    },
    {
        id: 'status',
        title: '订单状态',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center width-100',
        fastSort: false,
        hideQuery:true,
        hideQueryType:'eq',
        hideQueryValue:6,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (record.status == 1){
                return '<span style="background:#008000;padding:2px 5px;color:white;">匹配中</span>';
            }
            if (record.status == 2){
                return '<span style="background:#568058;padding:2px 5px;color:white;">交易中</span>';
            }
            if (record.status == 3){
                return '<span style="background:#0000FF;padding:2px 5px;color:white;">持仓中</span>';
            }
            if (record.status == 4){
                return '<span style="background:#FFDF9B;padding:2px 5px;color:white;">待平仓</span>';
            }
            if (record.status == 5){
                return '<span style="background:#008000;padding:2px 5px;color:white;">平仓中</span>';
            }
            if (record.status == 6){
                return '<span style="background:#FFDF9B;padding:2px 5px;color:white;">待结算</span>';
            }
            if (record.status == 7){
                return '<span style="background:black;padding:2px 5px;color:white;">已结算</span>';
            }
            if (record.status == 8){
                return '<span style="background:#747372;padding:2px 5px;color:white;">流单</span>';
            }
        }
    },
    {
        id: 'look',
        title: '审核',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            // var look = "<a href='" + ncGlobal.adminRoot + "orderHold/look?id=" + record.id + "' class='btn btn-sm btn-primary m-r-10' data-toggle='modal'><i class='fa fa-edit'></i>&nbsp;查看&nbsp;</a>";
            var verify = "<a data-target='#addModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "'><i class='fa fa-edit'></i>&nbsp;审核&nbsp;</a>";

            var html = "";
            // // 判断是否有编辑权限
            // if ($("#waitPermi") && $("#waitPermi").val() == 1) {
            //     html += look;
            // }
            if ($("#verifyPermi") && $("#verifyPermi").val() == 1) {
                html += verify;
            }
            return html;
        }
    },
    {
        id: 'pass',
        title: '一键通过',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            // var look = "<a href='" + ncGlobal.adminRoot + "orderHold/look?id=" + record.id + "' class='btn btn-sm btn-primary m-r-10' data-toggle='modal'><i class='fa fa-edit'></i>&nbsp;查看&nbsp;</a>";
            var verify = "<a class='btn btn-sm btn-primary m-r-10' onclick='_oneKeyPass(this)' data-toggle='modal' data-no='" + dataNo + "'><i class='fa fa-edit'></i>&nbsp;一键通过&nbsp;</a>";

            var html = "";
            // // 判断是否有编辑权限
            // if ($("#waitPermi") && $("#waitPermi").val() == 1) {
            //     html += look;
            // }
            if ($("#oneKeyPermi") && $("#oneKeyPermi").val() == 1) {
                html += verify;
            }
            return html;
        }
    },
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'order/list.json',
    exportFileName: '方案列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int:["id","userId","limit","investId","tradeCredit","status","sellDealNumber","inviterId"],
        Timestamp:["buyTime","acceptTime","buyDealTime","eveningUpTime","sellTime","sellDealTime","settleTime"],
        BigDecimal:["buyPrice","sellPrice","profitCount","profitStopPrice","loseStopPrice"]}
};

var grid = $.fn.DtGrid.init(dtGridOption);
//默认查询条件
grid.fastQueryParameters = new Object();
// grid.fastQueryParameters['eq_investor_id'] = $('#userId').val();
grid.fastQueryParameters['eq_status'] = 6;
//排序
grid.sortParameter.columnId = 'id';
grid.sortParameter.sortType = 1;
//列表结束

//操作处理开始
var OperateHandle = function () {

    function _bindEvent() {

        //新增对话框初始化
        $("#addModal").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".alert-danger").remove();
            $("#addForm").psly().reset();

            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            var modal = $(this);
            //获取列表框中的原始数据
            var gridData = grid.sortOriginalDatas[datano];
            $("#equityCode").val(gridData.equityCode);
            $("#equityName").val(gridData.equityName);
            $("#buyDealNumber").val(gridData.buyDealNumber);
            $("#buyDealPrice").val(gridData.buyDealPrice);
            $(".oId").val(gridData.id);
            $("#sellDealPrice").val(gridData.sellDealPrice);
            $("#sellDealNumber").val(gridData.sellDealNumber);
        });

        //编辑对话框初始化
        $("#editModal").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".alert-danger").remove();
            $("#editForm").psly().reset();
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            var modal = $(this);
            //获取列表框中的原始数据
            var gridData = grid.sortOriginalDatas[datano];

            $("#adminId").val(gridData.id);
            $("#adminName").val(gridData.name);
            modal.find("[name='password']").val("");
            modal.find("[name='password2']").val("");
            modal.find("[name='roleId']").val(gridData.roleId);
        });

        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters['eq_status'] = 6;
            grid.fastQueryParameters['lk_name'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });
    }

    //外部可调用
    return {
        bindEvent: _bindEvent,
    }
}();
//操作处理结束
/**
 * 一键通过js
 * @param event
 * @private
 */
function _oneKeyPass(event) {
    //获取接受事件的元素
    // var button = $(event.relatedTarget);
    // //获取data 参数
    // var datano = button.data('no');
    var datano = $(event).attr("data-no");
    //获取列表框中的原始数据
    var gridData = grid.sortOriginalDatas[datano];
    var id = gridData.id;
    $.ajax({
        async: false,
        url: ncGlobal.adminRoot + 'order/oneKeyPass.json',
        type: "post",
        data: {id: id},
        success: function (data) {
            if (data.code == 200) {
                //清除错误信息
                $(".alert-danger").remove();
                $.ncAlert({content: data.message, autoCloseTime: 2,
                    callback:function () {
                        window.location.reload()
                    }
                });
            } else {
                $.ncAlert({content: data.message, autoCloseTime: 2,
                    callback:function () {
                        window.location.reload()
                    }
                });
            }
        },
        error: function (e) {
            alert("错误！！");
        }
    });
}
$(function () {
    //加载列表
    grid.load();
    //页面绑定事件
    OperateHandle.bindEvent();
});